package io.github.javpower.gifweb.service;

import io.github.javpower.gifweb.entity.Template;
import io.github.javpower.gifweb.store.TemplateStore;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TemplateService {
    private final TemplateStore templateStore;

    public TemplateService() throws IOException, ClassNotFoundException {
        templateStore = new TemplateStore();
    }

    public Template saveTemplate(Template template)  {
        try {
            templateStore.saveTemplate(template);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return template;
    }

    public Template getTemplate(String templateId) {
        return templateStore.getTemplate(templateId);
    }

    public void deleteTemplate(String templateId)  {
        try {
            templateStore.deleteTemplate(templateId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Template> getAllTemplates() {
        return templateStore.getAllTemplates().values().stream().collect(Collectors.toList());
    }

    public void close() throws IOException {
        templateStore.close();
    }
}